package com.soft863.ct.consumer.bean;

import com.soft863.ct.consumer.dao.HBaseDao;
import com.soft863.tc.common.bean.Consumer;
import com.soft863.tc.common.constant.ValueConstant;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;

import java.io.IOException;
import java.time.Duration;
import java.util.Collections;
import java.util.Properties;

/*
 * 通话日志的消费者
 * */
public class CallLogConsumer implements Consumer {

    @Override
    public void consume() {
        try {
            //构建配置对象
            Properties configs = new Properties();
            configs.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("consumer.properties"));
            //获取flume采集的数据
            KafkaConsumer<String, String> consumer = new KafkaConsumer<>(configs);
            //订阅主题
            consumer.subscribe(Collections.singletonList(ValueConstant.TOPIC));
            HBaseDao dao = new HBaseDao();
            dao.init();
            //消费数据
            while (true) {
                ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100));
                for (ConsumerRecord<String, String> record : records) {
                    System.out.println(record.value());
                    dao.insertData(record.value());
//                    CallLog log = new CallLog(record.value());
//                    dao.insertData(log);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void close() throws IOException {

    }
}
